package cn.iocoder.yudao.module.member.service.technician;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.dict.core.util.DictFrameworkUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.app.technician.vo.AppTechnicianPageReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.comment.AppCommentDO;
import cn.iocoder.yudao.module.member.dal.dataobject.healthy.AppHealthyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.order.AppOrderDO;
import cn.iocoder.yudao.module.member.dal.dataobject.project.AppProjectDO;
import cn.iocoder.yudao.module.member.dal.dataobject.technician.AppTechnicianDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.comment.AppCommentMapper;
import cn.iocoder.yudao.module.member.dal.mysql.healthy.AppHealthyMapper;
import cn.iocoder.yudao.module.member.dal.mysql.order.AppOrderMapper;
import cn.iocoder.yudao.module.member.dal.mysql.project.AppProjectMapper;
import cn.iocoder.yudao.module.member.dal.mysql.technician.AppTechnicianMapper;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 理疗项目 Service 实现类
 *
 * @author 理疗系统
 */
@Service
@Validated
public class AppTechnicianServiceImpl implements AppTechnicianService {

    @Resource
    private AppTechnicianMapper appTechnicianMapper;

    @Resource
    private AppHealthyMapper  appHealthyMapper;

    @Resource
    private AppOrderMapper appOrderMapper;

    @Resource
    private MemberUserMapper memberUserMapper;

    @Resource
    private AppCommentMapper appCommentMapper;

    @Override
    @Transactional
    public PageResult<AppTechnicianDO> getTechnicianPage(AppTechnicianPageReqVO pageReqVO) {

        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
//        if (Objects.isNull(loginUserId)) {
//            throw new ServiceException(401,"请登录后再操作");
//        }
        MemberUserDO memberUserDO = memberUserMapper.selectById(loginUserId);

        // 截取用户区域编码的前两个，查询同区域的技师
        String[] areaCodeArr = memberUserDO.getAreaCode().split(",");
        pageReqVO.setAreaCode(areaCodeArr[0] + "," + areaCodeArr[1]);
        pageReqVO.setRole("technician");
        PageResult<MemberUserDO> memberUserDOPageResult = memberUserMapper.selectPage(pageReqVO);

        List<AppTechnicianDO> appTechnicianDOS = new ArrayList<>();
        memberUserDOPageResult.getList().parallelStream().forEach(item -> {
            AppTechnicianDO appTechnicianDO = appTechnicianMapper.selectOne(new LambdaQueryWrapperX<AppTechnicianDO>().eq(AppTechnicianDO::getMemberId, item.getId()));
            appTechnicianDO.setNickname(item.getNickname());
            appTechnicianDO.setAvatar(item.getAvatar());
            appTechnicianDO.setMobile(item.getMobile());
            appTechnicianDO.setStatusName(DictFrameworkUtils.getDictDataLabel("mini_technician_service_status", appTechnicianDO.getStatus()));
            appTechnicianDO.setCommentTotal(String.valueOf(appCommentMapper.selectCount(new LambdaQueryWrapperX<AppCommentDO>()
                    .eq(AppCommentDO::getTechnicianId, item.getId()))));

            appTechnicianDOS.add(appTechnicianDO);
        });
        return new PageResult<>(appTechnicianDOS, memberUserDOPageResult.getTotal());
    }

    @Override
    public AppHealthyDO getHealthyById(Long id) {

        Long commentTotal = appCommentMapper.selectCount(new LambdaQueryWrapperX<AppCommentDO>().eq(AppCommentDO::getTechnicianId, id));
        Long serviceTotal = appOrderMapper.selectCount(new LambdaQueryWrapperX<AppOrderDO>().select(AppOrderDO::getId).eq(AppOrderDO::getTechnicianId, id));
        AppHealthyDO appHealthyDO = appHealthyMapper.selectOne(new LambdaQueryWrapperX<AppHealthyDO>().eq(AppHealthyDO::getMemberId, id));
        MemberUserDO memberUserDO = memberUserMapper.selectById(id);
        appHealthyDO.setMemberName(memberUserDO.getNickname());
        appHealthyDO.setAvatar(memberUserDO.getAvatar());
        appHealthyDO.setServicesTotal(serviceTotal);
        appHealthyDO.setCommentTotal(commentTotal);
        return appHealthyDO;
    }

    @Override
    @Transactional
    public AppTechnicianDO getTechnicianById(Long id) {
        MemberUserDO memberUserDO = memberUserMapper.selectOne(new LambdaQueryWrapperX<MemberUserDO>().eq(MemberUserDO::getId, id));
        AppTechnicianDO appTechnicianDO = appTechnicianMapper.selectOne(new LambdaQueryWrapperX<AppTechnicianDO>().eq(AppTechnicianDO::getMemberId, id));
        return new AppTechnicianDO().setAvatar(memberUserDO.getAvatar()).setNickname(memberUserDO.getNickname())
                .setIntroduce(appTechnicianDO.getIntroduce());
    }
}
